package com.liujie.controller;

import com.liujie.common.R;
import com.liujie.pojo.Room;
import com.liujie.service.RoomService;
import com.liujie.websocket.WebSocketServer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/ws")
@CrossOrigin(origins = "*",maxAge = 3600)//解决跨域
public class WebSocketController {
    @Autowired
    private RoomService roomService;

    /**
     * 用于查询房间目前人数
     * @param roomId 房间号
     * @return 人数
     */
    @GetMapping("/{roomId}")
    public R roomNum(@PathVariable String roomId){
        Integer roomNum = WebSocketServer.roomNum(roomId);
        return R.success(roomNum);
    }

    /**
     * 删除房间，此处与RoomController中不同，此处还需要移除房间websocket通信
     * @param room 房间实体
     * @return 结果
     */
    @DeleteMapping
    public R deleteRoom(@RequestBody Room room){
        room.setDeleted(1);
        boolean b1 = roomService.updateById(room);
        boolean b = WebSocketServer.deleteRoom(room.getId());
        if (b && b1){
            return R.success("删除成功！");
        }
        return R.error("删除失败！");
    }
}
